import db from '../db/index.js'
import bcrypt from 'bcryptjs'
export function stuinfo(req, res) {
    const { stuid } = req.query
    const sql = `SELECT 
    student.stuid,
    student.name,
    student.IDcard,
    student.phone,
    student.startdate,
    class.cname AS class,
    submajor.name AS major_name,
    student.nationality,
    student.grade,
    teacher.name AS teacher
FROM 
    student
INNER JOIN 
    class ON class.classid = student.classid  -- Fixed typo (calssid → classid)
INNER JOIN 
    submajor ON submajor.submajorid = student.subid
INNER JOIN 
    teacher ON teacher.teacherid = submajor.teacherid
WHERE 
    student.stuid = ?`
    db.query(sql, stuid, (err, data) => {
        if (err) return res.dbno(err)
        const person = data[0]
        const sql2 = 'select honor,create_time from honor where stuid=?'
        db.query(sql2, stuid, (err, data) => {
            if (err) return res.dbno(err)
            res.send({ status: 200, person, honor: data.length != 0 ? data : [] })
        })
    })
}
export function tecinfo(req, res) {
    const { teacherid } = req.query
    const sql = `SELECT 
    teacher.teacherid,
    teacher.name,
    teacher.IDcard,
    teacher.phone,
    teacher.nationality,
    teacher.create_time,
    job.jobname AS job,
    submajor.name AS major
FROM 
    teacher
INNER JOIN 
    job ON job.jobid = teacher.jobid
INNER JOIN 
    submajor ON submajor.submajorid = teacher.subid
WHERE 
    teacher.teacherid = ?;
    `
    db.query(sql, teacherid, (err, data) => {
        if (err) return res.dbno(err)
        const person = data[0]
        const sql2 = 'select name,create_time from thonor where teacherid=?'
        db.query(sql2, teacherid, (err, data) => {
            if (err) return res.dbno(err)
            res.send({ status: 200, person, honor: data.length != 0 ? data : [] })
        })
    })
}
export function stucourse(req, res) {
    const { pagenum, pagesize, studentid } = req.body
    const page = (pagenum - 1) * pagesize

    const sql = 'select courselistid from student where stuid = ?'
    db.query(sql, studentid, (err, data) => {
        if (err) return res.dbno(err)
        if (data.length == 0) return res.cc(198, '没有改数据')
        // 获取的课程列表id是个'[1,2,3,4]'字符串，需要使用JSON.parse转换成功数组
        const courselistid = JSON.parse(data[0].courselistid)
        const sql2 = `SELECT 
    course.cname,
    course.ctime,
    course.credit,
    course.type,
    teacher.name AS teacher
FROM 
    course
INNER JOIN 
    teacher ON teacher.teacherid = course.teacherid
WHERE 
    course.courseid IN (?)
    limit ${pagesize} offset ${page}
        `
        db.query(sql2, [courselistid], (err, data) => {
            if (err) return res.dbno(err)
            console.log(data);

            res.send({ status: 200, course: data, total: courselistid.length })
        })
    })
}
export function teccourse(req, res) {
    const { pagenum, pagesize, teacherid } = req.body
    const page = (pagenum - 1) * pagesize
    const sql = `select courseid,cname,ctime,credit,type from course where teacherid = ?  limit ${pagesize} offset ${page}`
    db.query(sql, teacherid, (err, data) => {
        if (err) return res.dbno(err)
        const course = data
        const sql2 = 'select count(*) as total from course where teacherid = ?'
        db.query(sql2, teacherid, (err, data) => {
            if (err) return res.dbno(err)
            res.send({ status: 200, course, total: data[0].total })
        })
    })
}

export function setstupwd(req, res) {
    const { stuid, oldpwd, newpwd } = req.body
    const sql = 'select islogin,password from student where stuid =?'
    db.query(sql, stuid, (err, data) => {
        if (err) return res.dbno(err)
        if (data.length == 0) return res.cc(198, '账号不存在')
        if (data[0].islogin == 0) {
            if (oldpwd != data[0].password) return res.cc(197, '原密码错误')
            const password = bcrypt.hashSync(newpwd, 10)
            const sql2 = 'update student set password =? ,islogin=1 where stuid = ?'
            db.query(sql2, [password, stuid], (err, data) => {
                if (err) return res.dbno(err)
                if(data.changedRows!=1) return res.cc(197,"修改密码失败")
                    res.send({ status: 200, message: '密码修改成功' })
            })
        }
        else {
            const compareResult = bcrypt.compareSync(oldpwd, data[0].password)
            if (!compareResult) return res.cc(197, '原密码错误')
            const password = bcrypt.hashSync(newpwd, 10)
            const sql2 = 'update student set password =?where stuid = ?'
            db.query(sql2, [password, stuid], (err, data) => {
                if (err) return res.dbno(err)
              if(data.changedRows!=1) return res.cc(197,"修改密码失败")
                    res.send({ status: 200, message: '密码修改成功' })
            })
        }
    })
}